package com.glory.wxelectricitypda.adapter;
import android.support.annotation.Nullable;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.base.App;
import com.glory.wxelectricitypda.bean.RecipeListEntity;

import java.util.List;

public class PERecipeListAdapter extends BaseQuickAdapter<RecipeListEntity.RecipeEntity, BaseViewHolder> {

    private int mSelectorPos = -1;

    public PERecipeListAdapter(@Nullable List<RecipeListEntity.RecipeEntity> data) {
        super(R.layout.item_bin_info_list,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, RecipeListEntity.RecipeEntity item) {
        helper.setText(R.id.tvRecipeName,item.getRecipeName());
        helper.setText(R.id.tvVersion,item.getVersion());
        helper.setText(R.id.tvStatus,item.getStatus());
        helper.setBackgroundColor(R.id.llItemLayout,item.isSelector() ?
                App.getInstance().getResources().getColor(R.color.free_ui_glory_theme_color) :
                App.getInstance().getResources().getColor(R.color.white));

        LinearLayout mItemLayout = helper.getView(R.id.llItemLayout);
        mItemLayout.setOnClickListener(view -> {
            if (mSelectorPos == -1){
                mSelectorPos = helper.getAdapterPosition();
                item.setSelector(true);
            }else if (mSelectorPos == helper.getAdapterPosition()){
//                FreeApi_ToastUtils.showFreeToast("点击相同条目",mContext,true,0);
                getData().get(mSelectorPos).setSelector(false);
                item.setSelector(true);
                mSelectorPos = helper.getAdapterPosition();
            }else if (mSelectorPos != helper.getAdapterPosition()){
                getData().get(mSelectorPos).setSelector(false);
                item.setSelector(true);
                mSelectorPos = helper.getAdapterPosition();
            }

            notifyDataSetChanged();
        });
    }

    public RecipeListEntity.RecipeEntity getSelectorData(){
        if (mSelectorPos == -1){
            return null;
        }
        return getData().get(mSelectorPos);
    }
}
